#include "XPMColorPicker.h"
#include <wx/gdicmn.h>
#include <wx/dcclient.h>
#include <wx/brush.h>
#include <wx/pen.h>
#include <wx/tglbtn.h>
#include <wx/msgdlg.h>
#include <wx/colordlg.h>

DEFINE_EVENT_TYPE( wxEVT_TRANSPARENT_COLOR_CHANGED )
DEFINE_EVENT_TYPE( wxEVT_LINE_COLOR_CHANGED )
DEFINE_EVENT_TYPE( wxEVT_FILL_COLOR_CHANGED )

const long XPMColorPicker::ID_TOGGLEBUTTON1 = wxNewId();
XPMColorPicker::XPMColorPicker( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int style,
                                const wxValidator &validator, const wxString& name ) {
  init_colours();
  wxSize sBestSize;
  sBestSize = DoGetBestSize();
  SetMinSize( sBestSize );
  Create( parent, id, pos, sBestSize, wxTAB_TRAVERSAL, _T( "id" ) );
  ToggleButton1 = new wxToggleButton( this, ID_TOGGLEBUTTON1, _( " LINE " ), wxDefaultPosition, wxDefaultSize, 0,
                                      wxDefaultValidator, _T( "ID_TOGGLEBUTTON1" ) );
  bLineOn = true;
  ComputeDimensions( sBestSize );
  if( iHeight > sBestSize.GetHeight() ) {
    iHeight = sBestSize.GetHeight();
  }
  iLineColor = 0;  //black
  iFillColor = XPM_MAX_COLOR - 1; //white
  Connect( wxEVT_SIZE, ( wxObjectEventFunction )&XPMColorPicker::OnEventSize );
  Connect( wxEVT_PAINT, ( wxObjectEventFunction )&XPMColorPicker::OnEventPaint );
  Connect( ID_TOGGLEBUTTON1, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, ( wxObjectEventFunction )&XPMColorPicker::OnToggleButton1Toggle );
  Connect( wxEVT_ENTER_WINDOW, ( wxObjectEventFunction )&XPMColorPicker::OnMouseEnter );
  Connect( wxEVT_LEAVE_WINDOW, ( wxObjectEventFunction )&XPMColorPicker::OnMouseLeave );
  Connect( wxEVT_SET_CURSOR, ( wxObjectEventFunction )&XPMColorPicker::OnSetCursor );
  Connect( wxEVT_LEFT_DOWN, ( wxObjectEventFunction )&XPMColorPicker::OnMouseClick );
  Connect( wxEVT_RIGHT_DOWN, ( wxObjectEventFunction )&XPMColorPicker::OnMouseRClick );
  Connect( wxEVT_LEFT_DCLICK, ( wxObjectEventFunction )&XPMColorPicker::OnMouseDClick );
}

void XPMColorPicker::init_colours( void ) {
  iPaletteSize = XPM_MAX_COLOR;
  ColourArray = new wxColour[iPaletteSize];
  if( !ColourArray ) {
    return;
  }
  ColourArray[0] = wxTheColourDatabase->Find( _( "BLACK" ) );
  ColourArray[1] = wxTheColourDatabase->Find( _( "BLUE VIOLET" ) );
  ColourArray[2] = wxTheColourDatabase->Find( _( "MAGENTA" ) );
  ColourArray[3] = wxTheColourDatabase->Find( _( "VIOLET" ) );
  ColourArray[4] = wxTheColourDatabase->Find( _( "VIOLET RED" ) );
  ColourArray[5] = wxTheColourDatabase->Find( _( "MEDIUM VIOLET RED" ) );
  ColourArray[6] = wxTheColourDatabase->Find( _( "PINK" ) );
  ColourArray[7] = wxTheColourDatabase->Find( _( "PLUM" ) );
  ColourArray[8] = wxTheColourDatabase->Find( _( "PURPLE" ) );
  ColourArray[9] = wxTheColourDatabase->Find( _( "SALMON" ) );
  ColourArray[10] = wxTheColourDatabase->Find( _( "BLUE" ) );
  ColourArray[11] = wxTheColourDatabase->Find( _( "AQUAMARINE" ) );
  ColourArray[12] = wxTheColourDatabase->Find( _( "CADET BLUE" ) );
  ColourArray[13] = wxTheColourDatabase->Find( _( "CORAL" ) );
  ColourArray[14] = wxTheColourDatabase->Find( _( "CORNFLOWER BLUE" ) );
  ColourArray[15] = wxTheColourDatabase->Find( _( "MEDIUM AQUAMARINE" ) );
  ColourArray[16] = wxTheColourDatabase->Find( _( "MEDIUM BLUE" ) );
  ColourArray[17] = wxTheColourDatabase->Find( _( "CYAN" ) );
  ColourArray[18] = wxTheColourDatabase->Find( _( "DARK SLATE BLUE" ) );
  ColourArray[19] = wxTheColourDatabase->Find( _( "DARK TURQUOISE" ) );
  ColourArray[20] = wxTheColourDatabase->Find( _( "LIGHT BLUE" ) );
  ColourArray[21] = wxTheColourDatabase->Find( _( "LIGHT STEEL BLUE" ) );
  ColourArray[22] = wxTheColourDatabase->Find( _( "MEDIUM SLATE BLUE" ) );
  ColourArray[23] = wxTheColourDatabase->Find( _( "MEDIUM TURQUOISE" ) );
  ColourArray[24] = wxTheColourDatabase->Find( _( "MIDNIGHT BLUE" ) );
  ColourArray[25] = wxTheColourDatabase->Find( _( "NAVY" ) );
  ColourArray[26] = wxTheColourDatabase->Find( _( "SKY BLUE" ) );
  ColourArray[27] = wxTheColourDatabase->Find( _( "SLATE BLUE" ) );
  ColourArray[28] = wxTheColourDatabase->Find( _( "STEEL BLUE" ) );
  ColourArray[29] = wxTheColourDatabase->Find( _( "TURQUOISE" ) );
  ColourArray[30] = wxTheColourDatabase->Find( _( "KHAKI" ) );
  ColourArray[31] = wxTheColourDatabase->Find( _( "DARK GREEN" ) );
  ColourArray[32] = wxTheColourDatabase->Find( _( "DARK OLIVE GREEN" ) );
  ColourArray[33] = wxTheColourDatabase->Find( _( "FOREST GREEN" ) );
  ColourArray[34] = wxTheColourDatabase->Find( _( "MEDIUM FOREST GREEN" ) );
  ColourArray[35] = wxTheColourDatabase->Find( _( "GREEN" ) );
  ColourArray[36] = wxTheColourDatabase->Find( _( "GREEN YELLOW" ) );
  ColourArray[37] = wxTheColourDatabase->Find( _( "LIME GREEN" ) );
  ColourArray[38] = wxTheColourDatabase->Find( _( "MEDIUM SEA GREEN" ) );
  ColourArray[39] = wxTheColourDatabase->Find( _( "MEDIUM SPRING GREEN" ) );
  ColourArray[40] = wxTheColourDatabase->Find( _( "SPRING GREEN" ) );
  ColourArray[41] = wxTheColourDatabase->Find( _( "PALE GREEN" ) );
  ColourArray[42] = wxTheColourDatabase->Find( _( "SEA GREEN" ) );
  ColourArray[43] = wxTheColourDatabase->Find( _( "YELLOW" ) );
  ColourArray[44] = wxTheColourDatabase->Find( _( "YELLOW GREEN" ) );
  ColourArray[45] = wxTheColourDatabase->Find( _( "GOLD" ) );
  ColourArray[46] = wxTheColourDatabase->Find( _( "GOLDENROD" ) );
  ColourArray[47] = wxTheColourDatabase->Find( _( "MEDIUM GOLDENROD" ) );
  ColourArray[48] = wxTheColourDatabase->Find( _( "ORANGE" ) );
  ColourArray[49] = wxTheColourDatabase->Find( _( "ORANGE RED" ) );
  ColourArray[50] = wxTheColourDatabase->Find( _( "WHEAT" ) );
  ColourArray[51] = wxTheColourDatabase->Find( _( "RED" ) );
  ColourArray[52] = wxTheColourDatabase->Find( _( "ORCHID" ) );
  ColourArray[53] = wxTheColourDatabase->Find( _( "DARK ORCHID" ) );
  ColourArray[54] = wxTheColourDatabase->Find( _( "FIREBRICK" ) );
  ColourArray[55] = wxTheColourDatabase->Find( _( "INDIAN RED" ) );
  ColourArray[56] = wxTheColourDatabase->Find( _( "MEDIUM ORCHID" ) );
  ColourArray[57] = wxTheColourDatabase->Find( _( "BROWN" ) );
  ColourArray[58] = wxTheColourDatabase->Find( _( "MAROON" ) );
  ColourArray[59] = wxTheColourDatabase->Find( _( "THISTLE" ) );
  ColourArray[60] = wxTheColourDatabase->Find( _( "SIENNA" ) );
  ColourArray[61] = wxTheColourDatabase->Find( _( "TAN" ) );
  ColourArray[62] = wxTheColourDatabase->Find( _( "DARK GREY" ) );
  ColourArray[63] = wxTheColourDatabase->Find( _( "DARK SLATE GREY" ) );
  ColourArray[64] = wxTheColourDatabase->Find( _( "DIM GREY" ) );
  ColourArray[65] = wxTheColourDatabase->Find( _( "GREY" ) );
  ColourArray[66] = wxTheColourDatabase->Find( _( "LIGHT GREY" ) );
  ColourArray[67] = wxTheColourDatabase->Find( _( "WHITE" ) );
  cTransparent = *wxLIGHT_GREY;
}

XPMColorPicker::~XPMColorPicker() {
  if( ColourArray ) {
    delete[] ColourArray;
  }
}

void XPMColorPicker::OnEventPaint( wxPaintEvent& event ) {
  wxPaintDC dc( this );
  Paint( &dc );
}

void XPMColorPicker::Paint( wxDC *dc ) {
  if( !dc ) {
    return;
  }
  ClearBackground();
  wxPen pBlackPen( *wxBLACK, 1, wxSOLID );
  wxPen pGreyPen( *wxLIGHT_GREY, 1, wxSOLID );
  wxPen pWhitePen( *wxWHITE, 1, wxSOLID );
  wxPen pBlue( *wxBLUE, 1, wxSOLID );
  wxBrush bWhiteBrush( *wxWHITE, wxSOLID );
  dc->SetPen( pBlackPen );
  dc->SetBrush( bWhiteBrush );
  dc->DrawRectangle( 0, 0, iColourWidth * 2, iHeight );
  dc->SetPen( pGreyPen );
  dc->DrawLine( 1, 1, iColourWidth * 2 - 1, 1 );
  dc->DrawLine( 1, 1, 1, iHeight - 1 );
  dc->SetPen( pWhitePen );
  dc->DrawLine( iColourWidth * 2 + 1, iHeight - 2, iColourWidth * 2 - 2, 2 );
  dc->DrawLine( iColourWidth * 2 + 1, iHeight - 2, 2, iHeight - 2 );
  wxColour cFillColour;
  wxColour cLineColour;
  cFillColour = GetFillColour();
  cLineColour = GetLineColour();
  wxPen pOutlinePen( cLineColour, 2, wxSOLID );
  wxBrush bFillBrush( cFillColour, wxSOLID );
  dc->SetPen( pOutlinePen );
  dc->SetBrush( bFillBrush );
  dc->DrawRectangle( 4, 4, iColourWidth * 2 - 7, iHeight - 7 );
  dc->SetPen( pBlackPen );
  dc->SetBrush( bWhiteBrush );
  dc->DrawRectangle( iColourWidth * 2 - 1, 0, iColourWidth * 2, iHeight / 2 );
  dc->SetBrush( wxNullBrush );
  dc->DrawRectangle( iColourWidth * 2 - 1, iHeight / 2 - 1, iColourWidth * 2, iHeight / 2 + 1 );
  wxBrush bTransparentBrush( cTransparent, wxSOLID );
  dc->SetPen( pWhitePen );
  dc->SetBrush( bTransparentBrush );
  dc->DrawRectangle( iColourWidth * 2 + 1, 2, iColourWidth * 2 - 4, iHeight / 2 - 4 );
  long i, j, jMax, iIndex;
  jMax = iPaletteSize / iNbRows;
  if( iPaletteSize % iNbRows > 0 ) {
    jMax++;
  }
  iIndex = -1;
  for( i = 0; i < iNbRows; i++ ) {
    for( j = 0; j < jMax; j++ ) {
      iIndex++;
      if( iIndex >= iPaletteSize ) {
        break;
      }
      wxColour cColor;
      cColor = GetPaletteColour( iIndex );
      wxBrush bFBrush( cColor, wxSOLID );
      dc->SetBrush( bFBrush );
      dc->SetPen( pBlackPen );
      dc->DrawRectangle( iColourWidth * 4 + j * iColourWidth - 1, i * iColourHeight,
                         iColourWidth, iColourHeight
                       );
      dc->SetBrush( wxNullBrush );
    }
    if( iIndex >= iPaletteSize ) {
      break;
    }
  }
}

void XPMColorPicker::Repaint( void ) {
  wxClientDC dc( this );
  Paint( &dc );
}

void XPMColorPicker::OnEventSize( wxSizeEvent& event ) {
  wxSize s;
  s = event.GetSize();
  ComputeDimensions( s );
  ToggleButton1->SetSize( iColourWidth * 2, iHeight / 2, iColourWidth * 2 - 2, iHeight / 2 - 1 );
  Refresh( false, NULL );
  Update();
  event.Skip();
}

void XPMColorPicker::ComputeDimensions( wxSize s ) {
  int iNbSquares;
  int iIdealSize;
  int iStartRow;
  iWidth = s.GetWidth();
  iHeight = s.GetHeight();
  iStartRow = 2;
  iIdealSize = XPM_IDEAL_SIZE;
  for( iNbRows = iStartRow; iNbRows < iPaletteSize / 2; iNbRows++ ) {
    iNbSquares = iPaletteSize / iNbRows;
    if( iPaletteSize % iNbRows > 0 ) {
      iNbSquares++;
    }
    iColourWidth = iWidth / ( iNbSquares + 4 );
    iColourHeight = iColourWidth;
    iHeight = iColourHeight * iNbRows;
    if( iColourWidth >= iIdealSize ) {
      iColourWidth = iIdealSize;
      iColourHeight = iColourWidth;
      iHeight = iColourHeight * iNbRows;
      break;
    }
  }
  wxSize sMinSize;
  wxSize sMaxSize;
  wxSize sDefaultSize;
  int iMinHeight, iMaxHeight;
  sMinSize = ToggleButton1->GetMinSize();
  iMinHeight = sMinSize.GetHeight();
  if( iMinHeight < XPM_IDEAL_SIZE ) {
    iMinHeight = XPM_IDEAL_SIZE;
  }
  if( iMinHeight < iColourHeight ) {
    iMinHeight = iColourHeight;
  }
  iMinHeight = iMinHeight * 2;
  sMinSize.SetWidth( XPM_IDEAL_SIZE * ( ( iPaletteSize / iNbRows ) + 4 ) );
  sMinSize.SetHeight( XPM_IDEAL_SIZE * iNbRows );
  SetMinSize( sMinSize );
  sMaxSize = ToggleButton1->GetMaxSize();
  iMaxHeight = sMaxSize.GetHeight();
  if( iMaxHeight < iMinHeight ) {
    iMaxHeight = iMinHeight;
  }
  sMaxSize.SetHeight( iMaxHeight * iPaletteSize / 2 );
  if( sMaxSize.GetWidth() < XPM_IDEAL_SIZE * ( 4 + iPaletteSize ) ) {
    sMaxSize.SetWidth( XPM_IDEAL_SIZE * ( 4 + iPaletteSize ) );
  }
  SetMaxSize( sMaxSize );
}

wxSize XPMColorPicker::DoGetBestSize( void ) const {
  int iBestWidth, iBestHeight;
  wxSize sResult;
  iBestWidth = XPM_IDEAL_SIZE * ( ( iPaletteSize / 2 ) + 4 );
  iBestHeight = XPM_IDEAL_SIZE * 2;
  sResult.SetWidth( iBestWidth );
  sResult.SetHeight( iBestHeight );
  return( sResult );
}

wxColour XPMColorPicker::GetLineColour( void ) {
  if( iLineColor < -1 ) {
    return( *wxBLACK );
  }
  if( iLineColor >= XPM_MAX_COLOR ) {
    return( *wxBLACK );
  }
  if( iLineColor == -1 ) {
    return( cTransparent );
  }
  return( ColourArray[iLineColor] );
}

wxColour XPMColorPicker::GetFillColour( void ) {
  if( iFillColor < -1 ) {
    return( *wxWHITE );
  }
  if( iFillColor >= XPM_MAX_COLOR ) {
    return( *wxWHITE );
  }
  if( iFillColor == -1 ) {
    return( cTransparent );
  }
  return( ColourArray[iFillColor] );
}

int XPMColorPicker::GetLineColourIndex( void ) {
  return( iLineColor );
}

int XPMColorPicker::GetFillColourIndex( void ) {
  return( iFillColor );
}

void XPMColorPicker::SetLineColour( int iIndex, bool bPostEvent ) {
  int iIndex2;
  iIndex2 = iIndex;
  if( iIndex2 < -1 ) {
    iIndex2 = 0;
  }
  if( iIndex2 >= XPM_MAX_COLOR ) {
    iIndex2 = 0;
  }
  iLineColor = iIndex2;
  if( bPostEvent ) {
    wxCommandEvent eventColorChanged( wxEVT_LINE_COLOR_CHANGED, GetId() );
    eventColorChanged.SetEventObject( this );
    wxPostEvent( this, eventColorChanged );
  }
  Refresh( false, NULL );
  Update();
}

void XPMColorPicker::SetFillColour( int iIndex, bool bPostEvent ) {
  int iIndex2;
  iIndex2 = iIndex;
  if( iIndex2 < -1 ) {
    iIndex2 = 0;
  }
  if( iIndex2 >= XPM_MAX_COLOR ) {
    iIndex2 = 0;
  }
  iFillColor = iIndex2;
  if( bPostEvent ) {
    wxCommandEvent eventColorChanged( wxEVT_FILL_COLOR_CHANGED, GetId() );
    eventColorChanged.SetEventObject( this );
    wxPostEvent( this, eventColorChanged );
  }
  Refresh( false, NULL );
  Update();
}

int XPMColorPicker::GetPaletteSize( void ) {
  return( iPaletteSize );
}

bool XPMColorPicker::SetPaletteSize( int iSize ) {
  wxColour *NewArray;
  NewArray = new wxColour[iSize];
  if( !NewArray ) {
    return( false );
  }
  int i;
  if( iSize >= iPaletteSize ) {
    for( i = 0; i < iPaletteSize; i++ ) {
      NewArray[i] = ColourArray[i];
    }
    for( i = iPaletteSize; i < iSize; i++ ) {
      NewArray[i] = *wxBLACK;
    }
  } else {
    for( i = 0; i < iSize; i++ ) {
      NewArray[i] = ColourArray[i];
    }
  }
  delete[] ColourArray;
  ColourArray = NewArray;
  iPaletteSize = iSize;
  Refresh( false, NULL );
  Update();
  return( true );
}

void XPMColorPicker::SetPaletteColour( int iIndex, wxColour cColor ) {
  if( iIndex < 0 ) {
    return;
  }
  if( iIndex >= iPaletteSize ) {
    return;
  }
  if( !ColourArray ) {
    return;
  }
  ColourArray[iIndex] = cColor;
}

wxColour XPMColorPicker::GetPaletteColour( int iIndex ) {
  if( iIndex < 0 ) {
    return( *wxBLACK );
  }
  if( iIndex >= iPaletteSize ) {
    return( *wxBLACK );
  }
  if( !ColourArray ) {
    return( *wxBLACK );
  }
  return( ColourArray[iIndex] );
}

wxColour XPMColorPicker::GetTransparentColour( void ) {
  return( cTransparent );
}

void XPMColorPicker::SetTransparentColour( wxColour cColour, bool bPostEvent ) {
  cTransparent = cColour;
  if( bPostEvent ) {
    wxCommandEvent eventColorChanged( wxEVT_TRANSPARENT_COLOR_CHANGED, GetId() );
    eventColorChanged.SetEventObject( this );
    wxPostEvent( this, eventColorChanged );
  }
}

void XPMColorPicker::OnToggleButton1Toggle( wxCommandEvent& event ) {
  bLineOn = !bLineOn;
  if( bLineOn ) {
    ToggleButton1->SetLabel( _( " LINE " ) );
  } else {
    ToggleButton1->SetLabel( _( " FILL " ) );
  }
}

void XPMColorPicker::OnMouseEnter( wxMouseEvent& event ) {
  SetCursor( *wxCROSS_CURSOR );
  event.Skip();
}

void XPMColorPicker::OnMouseLeave( wxMouseEvent& event ) {
  SetCursor( *wxCROSS_CURSOR );
  event.Skip();
}

void XPMColorPicker::OnSetCursor( wxSetCursorEvent& event ) {
  event.SetCursor( *wxCROSS_CURSOR );
  event.Skip();
}

void XPMColorPicker::OnMouseDClick( wxMouseEvent& event ) {
  int iColourIndex;
  iColourIndex = GetColourIndex( event.m_x, event.m_y );
  if( iColourIndex == -1 ) {
    wxColourDialog cd( this );
    if( cd.ShowModal() == wxID_OK ) {
      wxColourData cColourData;
      wxColour cColour;
      cColourData = cd.GetColourData();
      cColour = cColourData.GetColour();
      SetTransparentColour( cColour );
      Refresh( false, NULL );
      Update();
    }
  } else if( ( iColourIndex > -1 ) && ( iColourIndex < iPaletteSize ) ) {
    wxColourDialog cd( this );
    if( cd.ShowModal() == wxID_OK ) {
      wxColourData cColourData;
      wxColour cColour;
      cColourData = cd.GetColourData();
      cColour = cColourData.GetColour();
      SetPaletteColour( iColourIndex, cColour );
      Refresh( false, NULL );
      Update();
    }
  }
  event.Skip();
}

void XPMColorPicker::OnMouseClick( wxMouseEvent& event ) {
  int iColourIndex;
  if( event.LeftDClick() ) {
    event.Skip();
    return;
  }
  iColourIndex = GetColourIndex( event.m_x, event.m_y );
  if( ( iColourIndex > -1 ) && ( iColourIndex < iPaletteSize ) && ( !( event.ButtonDClick( wxMOUSE_BTN_LEFT ) ) ) ) {
    if( bLineOn ) {
      iLineColor = iColourIndex;
    } else {
      iFillColor = iColourIndex;
    }
    Refresh( false, NULL );
    Update();
    if( bLineOn ) {
      wxCommandEvent eventColorChanged( wxEVT_LINE_COLOR_CHANGED, GetId() );
      eventColorChanged.SetEventObject( this );
      wxPostEvent( this, eventColorChanged );
    } else {
      wxCommandEvent eventColorChanged( wxEVT_FILL_COLOR_CHANGED, GetId() );
      eventColorChanged.SetEventObject( this );
      wxPostEvent( this, eventColorChanged );
    }
    return;
  } else if( ( iColourIndex == -1 ) && ( !( event.ButtonDClick( wxMOUSE_BTN_LEFT ) ) ) ) {
    if( bLineOn ) {
      iLineColor = -1;
    } else {
      iFillColor = -1;
    }
    Refresh( false, NULL );
    Update();
    return;
  }
  event.Skip();
}

void XPMColorPicker::OnMouseRClick( wxMouseEvent& event ) {
  int iColourIndex;
  if( event.LeftDClick() ) {
    event.Skip();
    return;
  }
  iColourIndex = GetColourIndex( event.m_x, event.m_y );
  if( ( iColourIndex > -1 ) && ( iColourIndex < iPaletteSize ) && ( !( event.ButtonDClick( wxMOUSE_BTN_RIGHT ) ) ) ) {
    if( !bLineOn ) {
      iLineColor = iColourIndex;
    } else {
      iFillColor = iColourIndex;
    }
    Refresh( false, NULL );
    Update();
    if( !bLineOn ) {
      wxCommandEvent eventColorChanged( wxEVT_LINE_COLOR_CHANGED, GetId() );
      eventColorChanged.SetEventObject( this );
      wxPostEvent( this, eventColorChanged );
    } else {
      wxCommandEvent eventColorChanged( wxEVT_FILL_COLOR_CHANGED, GetId() );
      eventColorChanged.SetEventObject( this );
      wxPostEvent( this, eventColorChanged );
    }
    return;
  } else if( ( iColourIndex == -1 ) && ( !( event.ButtonDClick( wxMOUSE_BTN_RIGHT ) ) ) ) {
    if( !bLineOn ) {
      iLineColor = -1;
    } else {
      iFillColor = -1;
    }
    Refresh( false, NULL );
    Update();
    return;
  }
  event.Skip();
}

int XPMColorPicker::GetColourIndex( int x, int y ) {
  wxSize sWindowSize;
  int iRow, iCol, iIndex, iNbSquaresPerRow;
  sWindowSize = GetSize();
  if( x < iColourWidth * 2 ) {
    return( -2 );
  }
  if( x > sWindowSize.GetWidth() ) {
    return( -2 );
  }
  if( y < 0 ) {
    return( -2 );
  }
  if( y > sWindowSize.GetHeight() ) {
    return( -2 );
  }
  if( x < iColourWidth * 4 ) {
    if( y < sWindowSize.GetHeight() / 2 ) {
      return( -1 );
    }
    return( -2 );
  }
  iRow = y / iColourHeight;
  iCol = ( x - iColourWidth * 4 ) / iColourWidth;
  iNbSquaresPerRow = iPaletteSize / iNbRows;
  if( iPaletteSize % iNbRows > 0 ) {
    iNbSquaresPerRow++;
  }
  iIndex = iRow * iNbSquaresPerRow + iCol;
  if( iIndex < iPaletteSize ) {
    return( iIndex );
  }
  return( -2 );
}
